package com.shiguiwu.springmybatis.data.json;

import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import javax.validation.constraints.NotBlank;
import java.util.Date;

/**
 * @description: 对象序列化成json
 * 验证是否需要声明字段才能序列化
 * @author: stone
 * @date: Created by 2022/12/9 10:13
 * @version: 1.0.0
 * @pakeage: com.shiguiwu.springmybatis.json
 */
@Slf4j
public class ObjToJsonTests {

    public static void main(String[] args) {
        Person xiao = new Person((byte) 1, "小秘", new Date());
        Person xiao2 = new Person((byte) 1, "小沟", new Date());


        String str = JSONUtil.toJsonStr(xiao); // {"sex":1,"name":"小秘","createTime":1670552412738} 不行
        log.info("序列化 ===> {}", str);


    }




    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class Person {
        private byte sex;
        @NotBlank(message = "名称不能为空！！")
        private String name;
        private Date createTime;

        public String getSexName() {
            return sex == 1 ? "男" : "女";
        }
    }
}
